﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://wpfimageviewer.codeplex.com
using System;
using System.Collections.Generic;
using BrainTechLLC.ImageOperatorsNonAForgeDependent;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using AForge.Imaging;
using AForge.Imaging.Filters;
using BrainTechLLC;
using BrainTechLLC.ImageOperators;

namespace BrainTechLLC.FilterExtensions
{
    public partial class FilterCollection : ObservableCollection<ISingleFilter>, ICanApply, INotifyPropertyChanged, IPostProcessor
    {
        public static FilterCollection Load(string path)
        {
            if (File.Exists(path))
            {
                try
                {
                    byte[] bytes = File.ReadAllBytes(path);
                    return Load(bytes);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                    throw;
                }
            }

            return null;
        }

        public static FilterCollection Load(byte[] bytes)
        {
            try
            {
                FilterCollectionSaved saved = bytes.DeserializeFromCompressedBinary(true) as FilterCollectionSaved;

                if (saved == null)
                    return null;

                List<ISingleFilter> singleFilters = saved.Filters;

                if (singleFilters != null)
                {
                    var collection = new FilterCollection();
                    collection.Active = saved.Active;
                    collection.CollectionName = saved.CollectionName;

                    foreach (var single in singleFilters)
                    {
                        SingleFilter sf = single as SingleFilter;
                        if (sf != null)
                        {
                            sf._propertyChanged = new FastSmartWeakEvent<PropertyChangedEventHandler>();
                        }
                        collection.AddFilter(single);
                    }

                    return collection;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                throw;
            }

            return null;
        }

        public void Save(string path)
        {
            var toSave = new FilterCollectionSaved(this);

            byte[] bytes = new byte[0];
            try
            {
                bytes = toSave.SerializeToCompressedBinary(true);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString());
                Debug.Assert(false);
            }
            File.WriteAllBytes(path, bytes);
        }
    }
}